`
https://leetcode.cn/problems/maximum-product-of-splitted-binary-tree/
`

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
var maxProduct = function (root) {
  // 先对整棵树求和，获取到整棵树的和
  const treeSum = (function getSum(root) {
    if (root === null) return 0
    return root.val + getSum(root.left) + getSum(root.right)
  })(root)
  let res = 0

  const getTreeSum = (root) => {
    if (root === null) return 0

    const leftSum = getTreeSum(root.left)
    const rightSum = getTreeSum(root.right)
    const rootSum = root.val + leftSum + rightSum

    // 整棵树剪掉以当前结点作为根的子树和，更新最大乘积
    res = Math.max(res, rootSum * (treeSum - rootSum))

    return rootSum
  }

  getTreeSum(root)

  return res % (1e9 + 7)
};